package com.tm.fiverole.security.handler;

import com.tm.fiverole.result.Result;
import com.tm.fiverole.security.domain.User;
import com.tm.fiverole.ulits.JwtTokenUtil;
import com.tm.fiverole.ulits.ResponseUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 新建一个登录成功的类
 */
@Component
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        User user = (User) authentication.getPrincipal();
        String jwtToken = JwtTokenUtil.generateToken(user);
        ResponseUtil.out(httpServletResponse, Result.SUCCESS(jwtToken));

    }
}